package com.example.administrator.shzs.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.FrameLayout;
import android.widget.RadioGroup;
import com.example.administrator.shzs.R;
import com.example.administrator.shzs.app.MyApplication;
import com.example.administrator.shzs.db.GoodsDataBaseInterface;
import com.example.administrator.shzs.db.OperateGoodsDataBase;
import com.example.administrator.shzs.fragment.HomeFragment;
import com.example.administrator.shzs.fragment.IntegralFragment;
import com.example.administrator.shzs.fragment.OrderFragment;
import com.example.administrator.shzs.fragment.UserFragment;

/**
 * 主界面
 */
public class MainActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {

    private FrameLayout Index_Fragment;
    private RadioGroup Index_RudioGroup;

    //四个界面
    private HomeFragment mIndexFragment;
    private IntegralFragment mIntegralFragment;
    private OrderFragment mOrderFragment;
    private UserFragment mUserFragment;

    /**
     * 操作数据库增删改查
     */
    private GoodsDataBaseInterface mGoodsDataBaseInterface;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MyApplication.app.activityList.add(this);
        mGoodsDataBaseInterface= OperateGoodsDataBase.getInstance();
        //初始化
        setViews();

        //默认显示第一个界面
        showFirstFragment();

        //监听器
        setListeners();

        //每次启动清空数据库
        clearGoodsCar();

    }

    /**
     * 清空购物车数据库
     */
    private void clearGoodsCar() {

        mGoodsDataBaseInterface.deleteAll(this);
        Log.e("TAG","---------数据库已清空！");

    }


    /**
     * 监听器
     */
    private void setListeners() {
        Index_RudioGroup.setOnCheckedChangeListener(this);
    }

    /**
     * 初始化控件
     */
    private void setViews() {
        Index_Fragment = (FrameLayout) findViewById(R.id.Index_Fragment);
        Index_RudioGroup = (RadioGroup) findViewById(R.id.Index_RadioGroup);
    }

    /**
     * 默认显示第一页
     */
    private void showFirstFragment() {
        //
        mIndexFragment=new HomeFragment();
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.add(R.id.Index_Fragment, mIndexFragment);
        ft.show(mIndexFragment);
        ft.commit();
    }


    /**
     * RadioButton的点击事件
     * @param group
     * @param checkedId RadioButton
     */
    public void onCheckedChanged(RadioGroup group, int checkedId) {

        //开启事物
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();

        switch (checkedId) {
            case R.id.Index_Index:

                if(mIndexFragment == null) {
                    mIndexFragment=new HomeFragment();
                    ft.add(R.id.Index_Fragment, mIndexFragment);
                }
                ft.show(mIndexFragment);

                //隐藏
                if(mIntegralFragment != null){
                    ft.hide(mIntegralFragment);
                }
                if(mOrderFragment != null){
                    ft.hide(mOrderFragment);
                }
                if(mUserFragment != null){
                    ft.hide(mUserFragment);
                }


                break;
            case R.id.Index_integral:


                if(mIntegralFragment == null) {
                    mIntegralFragment= new IntegralFragment();
                    ft.add(R.id.Index_Fragment, mIntegralFragment);
                }

                ft.show(mIntegralFragment);

                //隐藏
                if(mIndexFragment != null){
                    ft.hide(mIndexFragment);
                }
                if(mOrderFragment != null){
                    ft.hide(mOrderFragment);
                }
                if(mUserFragment != null){
                    ft.hide(mUserFragment);
                }

                break;

            case R.id.Index_Order:



                if(mOrderFragment == null) {
                    mOrderFragment= new OrderFragment();
                    ft.add(R.id.Index_Fragment, mOrderFragment);
                }
                ft.show(mOrderFragment);

                //隐藏
                if(mIndexFragment != null){
                    ft.hide(mIndexFragment);
                }
                if(mIntegralFragment != null){
                    ft.hide(mIntegralFragment);
                }
                if(mUserFragment != null){
                    ft.hide(mUserFragment);
                }

                break;

            case R.id.Index_User:


                if(mUserFragment == null) {
                    mUserFragment= new UserFragment();
                    ft.add(R.id.Index_Fragment, mUserFragment);
                }
                ft.show(mUserFragment);

                //隐藏
                if(mIndexFragment != null){
                    ft.hide(mIndexFragment);
                }
                if(mIntegralFragment != null){
                    ft.hide(mIntegralFragment);
                }
                if(mOrderFragment != null){
                    ft.hide(mOrderFragment);
                }

                break;
        }

        //提交事物
        ft.commit();
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        //监听返回键
        if(keyCode==KeyEvent.KEYCODE_BACK){

            //按下返回到桌面
            Intent desktop = new Intent(Intent.ACTION_MAIN);
            desktop.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            desktop.addCategory(Intent.CATEGORY_HOME);
            startActivity(desktop);
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

}
